The question of loyalty to the chain of command
The question of loyalty to the chain of command and civilian government in a rebellion seems somewhat misplaced, but in a strictly legalistic sense, he could not "surrender" his army absent the US forces forcing such a surrender due to military action - think Buckner at Fort Henry, or McCown at Island Number 10.
Having said that, morally, yes, he should have - of course, morally, he should not have rebelled in the first place, much less for the cause of the rebellion. But that's a different issue, and requires a Lee vastly different than he was historically.
However, having said that, he could have taken an action after Gettysburg that would have been within his legal options as a commissioned officer, albeit of a rebellion:
He could have resigned, and made public that he was resigning in protest of Davis' government continuing to resist.
Given the generally low level of tolerance for dissent in the CSA, I'd guess he would have ended up in Libby Prison pretty damn quickly, but it certainly would have made a point.
Best,