Why did David (in Walter's body) help kill the Xenomorph onboard the Covenant?
Couldn't he have it kill the rest of the crew and then trap it in one of the chambers?
I think David saw it as a test for the Xenomorph.
The only way to gauge how close to "perfect" his creation is, is to test it. He was in a good position to watch the Xenomorph and see how it reacted and tried to kill the remaining crew members. With any type of creation, organic or machine, the creators will always want to test it to see what needs to be improved.
I don't think David was overly concerned with pretending to be Walter at this point in the movie. If the Xenomorph killed the remaining crew then he gains control of the ship, if the remaining crew kill the Xenomorph then he "helped" and has their trust. Either way, he comes out of it with control of the ship.
David didn't seem to believe that the Xenomorph was a threat to him so being left along with it on the ship wouldn't be something he was concerned about had it successfully killed the crew.
First of all, it's more likely that David simply disguised himself as Walter rather than swapping bodies.
The reason that David went along with the humans' plan to kill the Xenomorph was so that the humans would go into hypersleep, trusting him to actually be Walter instead of David. That way, he has free reign over the thousands of people in stasis on board to continue his experiments without putting himself or two of his other test subjects at risk.
David didn't switch bodies, you can tell because he still has the damage to his face. I'm surprised he managed to short and dye his hair in such a short time. Equally perplexing is why Daniels didn't notice that David wasn't Walter, especially while she was helping him staple his face back together; You'd think she'd notice that he wasn't self-repairing like he should.... Also worth noting is that David was missing the nail hole under his chin. In my opinion, it was way to obvious that David had replaced Walter especially the way they cut away from the fight...
David's ultimate goal was to create life. Specifically, life that is "better" by some subjective measure than his own creators or their creators (Engineers). Everything else is secondary to that goal. He even cut off his own hand to impersonate Walter, that is how much nothing else mattered to him.
Think back to the scene where he shows the captain his experiments. He tells him he really wants to show him his successes, downstairs: he then says the eggs are waiting... for what? A host, obviously, as we all know from previous movies. He started out with a single host, Elizabeth Shaw, who was dead long before his creation was perfected. He needed another human to be the host to the facehugger to see what it would do. This is why he broadcast that song, to lure spaceships to the planet so he would have more hosts.
He learns that the ship is a colony ship, with hundreds of humans in cryosleep. Jackpot. Practically infinite fodder (hosts) for breeding the perfect organism. Keep in mind that so far, he only had two human hosts to work with and had great success: the ship has a hundred times as many.
Helping the crew is a means to an end: gaining control of the passengers on the colony ship. He could have revealed his secret early, except the crew already figured out that the xenomorphs use humans as incubators. They saw an alien burst from one of their own (the second one to be infected early in the movie, I do not remember his name), and Daniels saw his "artwork" showing aliens being born out of humans. It is not a great leap for them to suspect that David wanted the sleeping colonists for experimentation, or that he meant them harm at the very least. They may have even sacrificed themselves to stop him, or instructed Mother to lock David out of being able to control the ship, jeopardizing his greater plans.
Helping the crew gained their trust, even if it meant killing his creation. That is okay, however, because their trust is crucial to his plans: get the crew to go to cryosleep willingly, rendering them incapable of interfering. Once that is accomplished, he has hundreds of unwilling hosts for his xenomorphs as well as alien embryos representing the successful culmination of years of experimenting. Why sacrifice everything for a single alien when he could have hundreds of aliens? Maybe even better aliens, given enough time.
Sometimes it is better to lose the battle in order to win the war.
I understood it in such a way that if he disobeyed the remaining crew, or lied to them, and they somehow didn't get killed too quickly, Daniels (or Tennessee in case of the former's death) would understand what is happening and, as the captain, could inform Mother (the computer system of the Covenant) that David is a threat. So, David would be risking getting locked in where he was without any authority over the ship, which wouldn't be very good for him even if the crew died. So he chose to sacrifice the pet in order to be able to go on with his experiments witn higher probability.