As other answers have stated, it's the law. I'll just add some to the reasons for the law:
Every once in a while, a vehicle crossing the railroad tracks won't make it to the other side in time. The reasons include sudden drivetrain issues, getting stuck on uneven crossings, traffic ahead blocking the way, or simply not noticingsimple failure to notice the train, due to malfunctioning signals or inattention. Signals can malfunctionThis happens roughly 6,000 times a year in the US, or be missed16 times a day.
When it's a passenger car, including ones their owners call trucks, it's normally onlynot such a small incidentbig deal. Cars are quick to evacuate, only having 1-2 passengers per door, so only 1 in 10 train-car collisions results in fatalities. They are alsoBeing mostly sheet metal with relatively small rigid parts, soroad cars also present littlelimited danger to athe train, although that can change with high-speed rail. Most of these collisions go unnoticed by the public.
Commercial vehicles are more of a much bigger dealproblem. Buses specifically are slow to evacuate, typically taking over a minute. Commercial trucks use long strongthick beams in their chassis, which can be a problem if they get pulled under the train. There have been dozens of fatal accidents involving train-truck collisions. While most train derailments are not Hollywood-grade catastrophesdisasters, with the sudden stoppage cancars just stopping as their wheels scrape over the sleepers, it's still likely to injure or kill passengers or overturn cargo cars.
The latest such accident happened just a week ago: https://www.thelocal.de/20190508/at-least-20-injured-after-train-and-truck-collide-near-hamburg