When I need to distinguish different object in the same material applied to all the objects in the scene, I'm using Object info pass, subtract pass ID that I want to apply certain color, and connecting to mix shadier by factor and controlling the color, just as you can see on the screenshot. I prefer to just use only one material for everything since it's easier to manage than applying different material to each objects and use group and input output, Is there any easier way to do than what I'm doing here? My way of doing looks a bit complicated
If I were you, I would turn this into a group node:
That way you make your node tree clearer, you only have one node with just the index value to handle. And you can easily add this node in another material or link or append this group node between blend files, making your setup easily reusable.