Why use an opto-coupler?
Both transformers and opto-couplers can provide isolation between 'hot' and 'safe' regions of a circuit.

The difference is that opto-couplers are very small, cheap, and can work on simple DC, so can shift a logic signal from one side to the other with no fuss and no or few other components.

Transformers are big, expensive, and need AC to work, they cannot simply be inserted into a logic line. When you need the thing that a transformer does well, moving power from one side to the other, then you have to use a transformer. Otherwise, you use something smaller, cheaper and easier.