Yes, it's generally safe to spray water and clean car engines as many cars these days are equipped with weatherproof housings across the engine bay.
However, to wash your car's engine you need to take some precautions that are explained in the following slides.
Avoid spraying water on a hot engine, as the extreme temperature change can damage metal and plastic parts.
It is recommended to avoid using high-pressure water sprays. Avoid holding the hose or pressure washer directly to the engine.
Before washing, you can cover sensitive components like the alternator, battery, and air intake with plastic bags to protect them from water.
After cleaning, ensure the engine bay is thoroughly dried. Once it's ensured that the engine bay is dried, you can start the engine.
Considering a professional engine cleaning service will be a good idea. Professionals have the right tools and knowledge to clean safely.