isolating between two power
If you've got the same ground on both sides the optocoupler adds very little. the magnitude of signal that can flow back theough the base resistor if you'd connected R1 straght to Q2 is very small.

I'm using a raspberry pi to control some 12V relays with just a resistor (330 ohms) and a transistor (BC547) and a diode parallel with the relay coil.

With the diode there that will stop sparking (unless you mean sparking in the relay contacts) and to keep that out is a lot more work.

If sparking of the relay contacts is the problem you need a snubber.

For a separate power supply use a second regulator to power the relay, or connect before the regulator that is used for the MCU