It's a fire hazard because there are a lot of variables and the code (and remember that the code is the law) errs on the side of safety.
EDIT: Consider this answer to another question that was asked today: https://diy.stackexchange.com/a/86019/24137
One answer referenced old knob and tube wiring being spliced inside walls and never showing a scorch mark in 100 years of use.
I'll buy that, because the connections were very solid, professional connections.
But also consider that k&t splices are soldered together in addition to being crimped and/or twisted together. Those soldered connections also protect the conductors within the splice itself from corrosion since oxygen is sealed out (both copper and aluminum wire oxidizes), and from movement. As wire changes temperature, it expands and contracts and this can loosen connections.
Also, it's possible that somebody will pull on one end of the cable or the other from a box, and a wirenut connection is more likely to fail than an intact run of wire is in that situation.
Compared to soldered connections, wire nuts are straight pressure connectors and it's far more likely that a wire nut connection will become loose and overheat or start sparking than it is for a soldered connection to do so.
Also, what if somebody does something like connecting a copper and an aluminum wire with a wire nut? Even the purple wire nuts specifically designed for making those bimetallic connections have a notoriously high failure rate (there are clamp-type connections that are better). Copper and aluminum expand and contract at different rates as the temperature changes, and this can really loosen the connection. Additionally, bimetallic connections like that are subject to greater levels of corrosion, and aluminum oxide is an insulator. Forcing current through an insulator generates a lot of heat (fire hazard). If that kind of failure happens inside a junction box, you'll smell it and be able to find and access it much more easily than if it was sealed up inside the wall. Additionally, the electrical box itself has some nominal fire resistance and code says the box has to extend far enough out to the surface of the wall so that no flammable materials are exposed around the edges of the box.
There are more reasons than these, of course.
So it actually is a fire safety issue, as most of the electrical code is.