Please read everything, there are some updates starting from QGIS 3.4 to QGIS 3.18.
In QGIS 3.4, you can build your attribution label automatically, according to layers which are visible.
First, you need to setup metadata for all your layers.
In QGIS 3.4.0 or 3.4.4, you had to use the QGIS Server
panel and fill the attribution
field:

Since QGIS 3.4.5, you can use the Metadata
panel:

In my project, I have 3 layers, but 2 layers have the same attribution Provider 2
.
Then, in your layout, give an itemID
to your map:

Then, in your label, you can create an expression like this:
array_to_string( -- 4 we make the attribution list unique and convert to string
array_distinct(
array_foreach( --2 for each layer in the map1
map_get(item_variables('map1'),'map_layers') --1 get the list of layers in the map1
,array_to_string(
-- 3 we fetch the attribution for the layer
layer_property(@element,'attribution')))))
Try to understand the expression by reading comments from 1 to 4.
This will render the correct attribution based on layers which are visible in my map. It will also remove attributions which are the same across layers.

In QGIS 3.6.0, you can add the array_sort
expression to sort alphabetically.
In QGIS 3.18, there is now a map_credits( 'Main Map' )
expression function which does all the array_foreach
, layer_properties
etc ... !