Just to clarify, private/hidden layers/groups will not appear in the Layers Panel, but they can still be seen in the canvas or used in joins/relations with other layers.
Native approach (QGIS API)
You need at least QGIS v3.18.
Using the native approach, you can make a layer 'Private' (or hidden) in this way:
layer = iface.activeLayer()
layer.setFlags(QgsMapLayer.Private)
See Making layer private/hidden/invisible in QGIS Layers Panel without Python to do this using the QGIS GUI.
However, using the native approach there is no way of making a layer group Private (or hidden). For that you would need the Plugin approach.
Plugin approach
You need at least QGIS v3.4.
I find making layers/groups hidden/invisible useful to avoid showing lookup tables, which are required for value relations but might make the Layers Panel look a bit overloaded.
ANSWER:
Use the plugin Invisible layers and groups, which allows you to:
- Make selected layers/groups hidden/invisible (plugin button).
- Make layers/groups (that are hidden/invisible) visible again (plugin button).
- Store layers/groups visibility in your QGIS project.
- Recreate layers/groups visibility when opening your QGIS project.
- Make a particular layer hidden/invisible (by code).
- Make a particular group hidden/invisible (by code).
Making a layer hidden/invisible (by code):
if 'InvisibleLayersAndGroups' in qgis.utils.plugins:
ilg = qgis.utils.plugins['InvisibleLayersAndGroups']
ilg.hideLayer( layer ) # layer is a QgsMapLayer
Making a group hidden/invisible (by code):
if 'InvisibleLayersAndGroups' in qgis.utils.plugins:
ilg = qgis.utils.plugins['InvisibleLayersAndGroups']
ilg.hideGroup( group ) # group is a QgsLayerTreeGroup
ilg.hideGroup( 'group2' ) # You can also pass a group name
Note: Depending on your use case, stick to one approach: either native or plugin, but don't use both at the same time.