This is done whenever there is a car on that track has a reason that it can't be moved. Say there's maintenance workers working on the car that could be in danger if the car was moved, or if a car is parked on the track and connected to "shore power" where the car's electrical system is still powered without an attached locomotive by connecting wires from an electrical panel next to the tracks. Moving a car that's already connected to "shore power" would rip the wires out and cause damage. The blue flag can also be used if a car or locomotive has something wrong with it that could cause more damage if moved. It's basically a sign that says "do not attempt to hitch onto this car or move it". With the blue lights, there's no chance that they won't see this at night.
Trains aren't dangerous, it's lack of common sense that's dangerous.