Two different objects at first then merge them, but keep them seperate on the former frames
There are a couple of techniques that could be used:

make a new Empty object E that is a child of object A that represents the position of object B after B and A lock together.  Give B a Copy Transforms constraint from Empty E, but keyframe its influence so it is 0 before they lock together and 1 after they lock together.
Keyframe A and B independently for the beginning, and after they lock together, you only have to keyframe A because B will copy its transform from empty E which moves with A because E is a child of A

Make 3 objects: A, B, and C.  C is a combination of a copy of A and a copy of B.  A and B are animated and renderable (the camera icon of the outliner) for the first part of the animation, but C is not renderable.  Then at the point where they combine A and B are keyframed to stop being renderable, and C is keyframed to become renderable, and then C is keyframed to move through the second part of the animation.
To create C you will need to make copies of A and B (shift-d duplicates, not linked duplicates) and then combine those copies without altering the original A and B.