They had agreed before so much as kissing that it wasn't adultery if they were in character when they did it. David, being something of a sad fanboy, sort of wondered if this made their passionate liasons canonical, but Catherine wasn't, as her catchphrase ran, bothered by such trivialities. Anyway, it wasn't like David was going to mention it in an episode of Doctor Who Confidential. (Though he had, once, fallen over a comment about Donna being the Doctor's "breast friend.")
They didn't have to be in costume, just in character. Both were actor enough to be able to keep to their roles in the throes of passion. Any serious professional would agree that at least technically it was the Doctor and Donna fucking rather than Catherine and David.
David knew, of course, that they were following in the footsteps of Tom and Lalla, at least in the sex part if not in the doomed marriage aspect. David really didn't want a doomed marriage. To anyone. He had only been joking about shagging Simm to better understand their characters, but there had been that one time when they woke up naked and hungover and had agreed never to try to remember what had happened the night before. Either way, the sexual tension had been all over the screen, and David couldn't help but be a bit proud of that.
The Doctor and Donna weren't and never had been in love. They never would be, unless Russell changed the finale scripts at the last moment. But they did have a certain... something. A strong hint of something more than mutual platonic adoration. David was at heart an Asexualist where the Doctor was concerned, but... well, Donna was an attractive woman, and the Doctor was so terribly lonely. And after losing Jenny something must have cheered him up before the following episode. This was the kind of character stuff that David tended to worry about.
So they had shagged, in-character. He wondered if Catherine herself was as enthusiastic (and loud) a lover as Donna obviously was, and he was vaguely concerned that he might started to develop a fetish, being unable to orgasm unless his partner was calling him a "skinny alien sexpuppet." Unlikely, but he'd heard stories about the Torchwood cast that could be considered cautionary tales.
It was all okay, because it was the Doctor and Donna fucking, not them. They'd even done it against the TARDIS console after hours, David muttering technobabble as the Doctor did his companion on a centuries-old Gallifreyan time-ship. He insisted in interviews that the Doctor wouldn't sleep with a companion, but there were things about the Doctor that the public didn't need to know. So he kept the shagging as secret as the backstory he'd invented for why the Doctor was so harsh to the Family of Blood and why the Doctor was such a bitch to Mickey. David liked it when his character made sense, even if no one else cared.
And, as much as he might try to intellectualise it, Catherine/Donna had great tits. The Doctor would have to be blind not to notice them, and completely sexless not to want to touch them and lick them and bite them gently as he fucked her against a wall. If the Doctor couldn't get another Time Lord, he could at least get his hands on a nice pair of tits. They were what kept the Doctor sane, most likely.
Catherine had her own ideas about the sex. They didn't talk about it much, but she'd told him that Donna was in it for the forbidden thrill of screwing an alien. She was a bit repulsed at first, but had come to look forward to the times when the Doctor looked a bit down and needed a good shag to cheer him up. The Doctor was a sensitive lover with a nice cock, and that was all Donna asked for in her current situation. She wasn't going to get hurt again, unless it was, say, some consensual spanking while slightly tipsy. (David was keen to explore that aspect of the Doctor-Donna relationship, but the opportunity had yet to properly present itself.)
It made sense for their characters, and that's why them shagging sometimes was just fine and dandy. And they didn't do it in costume, but sometimes they used the sonic screwdriver.