Is it legal to build your own plane and fly it?
Yes, it's legal. The FAA has an amateur-built aircraft homepage with links to regulations, guidance etc.
It's also very common. The Experimental Aircraft Association has a lot of resources too.