michaelday:
Just so I can get my head around it for the future, for what sorts of real-world applications should you choose an optocoupler instead of a relay?
Optocouplers typically have higher switching speeds than relays. This is true for basic, "commodity" optocouplers, but you can also buy high-speed optocouplers that are designed for e.g. data transmission. If you needed high switching speed, such as if you were passing through a high-frequency PWM signal or you were passing a high-speed data signal, an optocoupler would be appropriate.
Low-end optocouplers are cheaper than relays, so if you can use an optocoupler, you might choose to do it to save money.
Relays and most optocouplers are non-linear. They are designed to be either fully on or off. So there is this transition period where the voltage is high enough on the control side that the device is starting to open up, but is not yet fully open, but there is no reliable relationship between the control-side voltage and the load-side voltage. What if you have an analog signal that you need to pass through? Like, you want 0-5v on the control side to map to a corresponding percentage of 0-Vcc on the load side. For example, maybe you are doing some kind of audio amplification for which you need isolation. Or maybe you need to drive an analog circuit via a DAC, but the analog circuit and the DAC must be isolated from each other. In that case, you can get a linear optoisolator. Linear relays don't exist; they're designed to be fully on or fully off.
You can get optoisolators with base leg pins that let you tune the speed/responsiveness of the chip. As far as I know, relays don't do this.
On the flip-side, relays are generally simpler and more robust than optocouplers. Relays can also usually handle higher currents.
Bear in mind that the distinctions between the two start to blur a little when you start using solid-state relays instead of mechanical relays like reed switches or solenoids. Solid-state relays are basically optocouplers that have been designed to act like mechanical relays. But inside, they're still optocouplers.