Generate vertex groups from objects
Give object A a vertex group named A. In edit mode, select all vertices and assign them to that group.
Same thing for object B with a group named B.
Then join A and B like normal. The vertex groups are preserved.

Script to do this automatically. Select the objects you want to join, make sure you're in Object mode, then run it from the text editor.
import bpy
assert bpy.context.mode == 'OBJECT'
for ob in bpy.context.selected_objects:
    if ob.type != 'MESH': continue
    vg = ob.vertex_groups.new(name=ob.name)
    vg.add(list(range(len(ob.data.vertices))), 1.0, 'REPLACE')
bpy.ops.object.join()