Traverse all elements and temporarily change their layer via ::Layer property for the ones in layer a or b. Then save dxf and revert the changes. This would require traversing of every entity which could have layer (may be quite a lot of code)
Another approach is to layer-by-layer hide all layers except of the ones required to be merged (i.e. a and b) using ILayer::Visible property. So only those entities are visible. Then select all entities (you can use IModelDocExtension::SelectAll method). Now use the following example: Change Layer For Selected Objects In Drawing to change the layer for all selected entities to the merged one (ab). Now save dxf and undo changes.
One of the possible approaches you can take is:
Another approach is to layer-by-layer hide all layers except of the ones required to be merged (i.e. a and b) using ILayer::Visible property. So only those entities are visible. Then select all entities (you can use IModelDocExtension::SelectAll method). Now use the following example: Change Layer For Selected Objects In Drawing to change the layer for all selected entities to the merged one (ab). Now save dxf and undo changes.