That is always legal, but less efficient. In a model S with the induction motor(s), for neutral, it completely shuts down the drive signal and the motor(s) free-wheel. But when you keep it in drive with the throttle in the "sweet spot" between power and regen, the inverter instead creates a synchronous field with zero slip between rotor and stator. It does that so that it is smooth and continuous with no steps or bumps as the load and/or accelerator pedal changes. But to do that, the inverter needs to generate a rotating field and so it still has all of the switching losses and some of the resistance loss in the stator coils and driver. Neutral avoids those losses altogether and ensures "perfect" coasting which your foot will never get exactly.
For the model 3, with its PM motor, again neutral can simply open-circuit the stator drive and have no switching or resistive losses (other than eddy currents (which are small but omnipresent when moving). In drive, the inverter continues to generate a synchronous field that matches the back-EMF so that, once again, everything is smooth and continuous between power and regen with no steps or bumps.
While most (all?) states outlaw coasting downhill in neutral, they generally do not prohibit the use of neutral on level or up-hill roads. Tesla's coast beautifully on level (or ever-so-slightly downhill) roads. Optimal hypermiling is achieved by accelerating slightly above the desired speed then coasting until slightly below it.