Can’t you trivially turn off a transponder?
But, if the transponder is installed, you are legally required to turn it on at all times no matter where you are (see 91.215(b)(3), "all controlled airspace" means everything above 700-1200ft AGL (depending on exactly where)).
So for this to be legal, you can't just turn if off: you have to actually remove it from the airplane. Whether that is legal additionally depends on the airplane.
https://www.law.cornell.edu/cfr/text/14/91.215 https://www.law.cornell.edu/cfr/text/14/91.225