Probably not legal.
Generally things that are illegal are illegal because enough people have maimed or killed themselves with it in ways that are not “common sense”. For example, you can’t simply have electrical wire stapled to the bottom of the joists in the basement because people might try to hang clothes off of them.
It's legal and done quite often in industrial installations - look around the next time the lights are up at your favorite restaurant, for example.
It is more expensive, by more than you'd think, and so it's rarely done.
It also allows all of the trades save the drywaller/painter to be rough and tumble with what they're doing; it doesn't have to look nice behind the walls.