No one here is raising the need for power or interference isolation. In the general case, optocouplers are not used to control relay coils and only special optoisolators are actually suitable for this. Such a solution is more expensive, takes up more space, and is insecure due to the uncertainty of the optoisolator's transmittance. Finally, it puts quite a heavy load on the digital port unnecessarily. Of course, you can special, better etc etc but here the idea was to have a simple, cheap and reliable circuit.
The use of an opto-isolator does not guarantee safety from interference. Safety from interference is guaranteed by the designer's knowledge.
Why this circuit is weak has already been written, and what it should be replaced with has also already been written.
Any improvement of any circuit inevitably involves complicating it. This is why real professional equipment is so packed with "meat".
A circuit should be complicated only when necessary and only to the extent necessary. You should start with the optimal solution and not with a poor one and then improve (or rather repair) it, if at all possible.
A good design is one that works correctly with as few harsh boundary conditions as possible. Ideally, under all conditions. A great many amateur circuits work only because the parameters of the components used and the operating conditions have happily "aligned". These are bad solutions, even though they work.
Janusz_kk wrote: the transistor is a full-fledged transistor only the gate is controlled a little differently than usual.
As you can see, servicing automation even for a lifetime does not make a person an electronics engineer. So does a badge on a suit or a position held. And not to be surprised, I know that this is rather the rule. It's just a pity that such 'revelations' become part of the online knowledge resource that students try to draw on. And then we are surprised that everything goes downhill.