If it’s a prescribed drug and it’s legal at both ends and all transit points of your trip, it’s fine to take on board, just check Customs regulations beforehand and bring your prescription with you. I would not recommend trying out a new medication on a plane for the first time though, and combining both Xanax and Ambien sounds like a bad idea.
FWIW, I was also prescribed Ambien for a long flight once. No particularly dramatic side effects, but instead of sleeping properly I felt like I was drifting in and out of consciousness, without being able to control when I slept, and felt like a complete zombie while I was awake. It was rather disconcerting, I didn’t feel rested on arrival, and I would have been quite useless if there had been any emergency/diversion/etc in flight, so I never tried it again. YMMV.