Python - Script to Assign Materials based on Mesh Name
Please try running this and let me know if there are any errors: (I just hope I understand your requirement correctly :)

import bpy

for obj in bpy.data.objects:
    objName = obj.name
    mat = bpy.data.materials.get(objName)
    if(mat != None):
        if(mat.name not in obj.data.materials):
            obj.data.materials.append(mat)
            obj.active_material_index = len(obj.data.materials)-1
        else:
            obj.active_material_index =  [*obj.data.materials].index(mat)
    else:
        print('No material available for', objName)